<?php

/**
 * iCMS - i Content Management System
 * Copyright (c) 2007-2017 iCMSdev.com. All rights reserved.
 *
 * @author icmsdev <master@icmsdev.com>
 * @site https://www.icmsdev.com
 * @licence https://www.icmsdev.com/LICENSE.html
 */
defined('iPHP') or exit('What are you doing?');
self::head();
?>
<script type="text/javascript">
    iCMS.set('Vue.data', {
        pid: "<?php echo $_GET['pid']; ?>",
        cid: "<?php echo $_GET['cid']; ?>",
        orderby: "<?php echo $_GET['orderby']; ?>",
        sub: "<?php echo $_GET['sub']; ?>",
        status: "<?php echo $_GET['status']; ?>",
        tfsub: "<?php echo $_GET['tfsub']; ?>",
        tcid: "<?php echo $_GET['tcid']; ?>",
    });
    iCMS.set('batch', {
        mvtcid: function() {
            var select = $("#tcid").clone().show()
                .removeClass("form-control")
                .attr("id", iCMS.random(3));
            $("option:first", select).remove();
            return select;
        }
    });
    $(function() {
        $("#import").click(function(event) {
            var import_wrap = document.getElementById("import_wrap");
            iCMS.ui.dialog({
                title: 'iCMS - 批理导入标签',
                content: import_wrap
            });
        });
        $("#local").click(function() {
            $("#localfile").click();
        });
        $("#localfile").change(function() {
            $("#import_wrap form").submit();
            $(this).val('');
        });
    });
</script>
<div class="content" id="<?php echo APP_MAINID; ?>">
    <div class="block">
        <div class="block-header block-header-default">
            <h3 class="block-title">搜索</h3>
            <div class="block-options">
                <div class="block-options-item">
                    <button class="btn btn-success btn-sm" type="button" id="import"><i class="fa fa-fw fa-send"></i> 批理导入标签</button>
                </div>
            </div>
        </div>
        <div class="block-content block-content-full">
            <form action="<?php echo iPHP_SELF; ?>" method="get">
                <input type="hidden" name="app" value="<?php echo Admincp::$APP_NAME; ?>" />
                <input type="hidden" name="do" value="<?php echo Admincp::$APP_DO; ?>" />
                <input type="hidden" name="uid" value="<?php echo $_GET['uid']; ?>" />
                <div class="form-row">
                    <div class="input-group input-group-sm col-sm-6 col-xl-4 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="cid">栏目</label>
                        </div>
                        <select v-model="cid" class="form-control" id="cid" name="cid">
                            <option value=""></option>
                            <option value="0">所有栏目</option>
                            <?php echo $cid_select =  Node::setAccess('cm')->select(); ?>
                        </select>
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <div class="custom-control custom-checkbox">
                                    <input v-model="sub" type="checkbox" class="custom-control-input" id="sub" name="sub">
                                    <label class="custom-control-label" for="sub">子栏目</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-xl-4 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="tcid">分类</label>
                        </div>
                        <select v-model="tcid" class="form-control" id="tcid" name="tcid">
                            <option value=""></option>
                            <option value="0">所有分类</option>
                            <?php echo $tcid_select =  Node::set('APPID', self::$appId)->setAccess('cm')->select(); ?>
                        </select>
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <div class="custom-control custom-checkbox">
                                    <input v-model="tfsub" type="checkbox" class="custom-control-input" id="tfsub" name="tfsub">
                                    <label class="custom-control-label" for="tfsub">子分类</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-xl-2 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="pid">属性</label>
                        </div>
                        <select v-model="pid" name="pid" id="pid" class="form-control">
                            <option value=""></option>
                            <option value="all">所有属性</option>
                            <option value="0">标签文章[pid='0']</option>
                            <?php echo $pid_option = PropWidget::getOption("pid"); ?>
                        </select>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-xl-2 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="status">状态</label>
                        </div>
                        <select v-model="status" name="status" id="status" class="form-control">
                            <option value="">所有状态</option>
                            <?php echo PropWidget::option(Tag::$statusMap, 'status'); ?>
                        </select>
                    </div>
                </div>
                <div class="form-row">
                    <div class="input-group input-group-sm col-md-6 mt-1">
                        <div class="input-group-prepend">
                            <span class="input-group-text">发布时间</span>
                        </div>
                        <input type="text" class="form-control js-flatpickr" name="starttime" value="<?php echo $_GET['starttime']; ?>" placeholder="开始时间" />
                        <div class="input-group-prepend">
                            <span class="input-group-text">-</span>
                        </div>
                        <input type="text" class="form-control js-flatpickr" name="endtime" value="<?php echo $_GET['endtime']; ?>" placeholder="结束时间" />
                    </div>
                    <div class="input-group input-group-sm col-md-6 mt-1">
                        <div class="input-group-prepend">
                            <span class="input-group-text">添加时间</span>
                        </div>
                        <input type="text" class="form-control js-flatpickr" name="post_starttime" value="<?php echo $_GET['post_starttime']; ?>" placeholder="开始时间" />
                        <div class="input-group-prepend">
                            <span class="input-group-text">-</span>
                        </div>
                        <input type="text" class="form-control js-flatpickr" name="post_endtime" value="<?php echo $_GET['post_endtime']; ?>" placeholder="结束时间" />
                    </div>
                </div>
                <div class="form-row">
                    <div class="input-group input-group-sm col-sm-6 col-lg-4 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="st">查找方式</label>
                        </div>
                        <select v-model="st" name="st" id="st" class="form-control">
                            <option value=""></option>
                            <option value="title">标题</option>
                            <option value="tag">标签</option>
                            <option value="source">出处</option>
                            <option value="clink">自定义链接</option>
                            <option value="id">ID</option>
                            <option value="weight">置顶权重</option>
                            <option value="tkd">标题/关键字/简介</option>
                            <option value="pic">缩略图</option>
                        </select>
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <div class="custom-control custom-checkbox">
                                    <input v-model="pic" type="checkbox" class="custom-control-input" id="pic" name="pic">
                                    <label class="custom-control-label" for="pic">缩略图</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-lg-2 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="orderby">排序</label>
                        </div>
                        <select v-model="orderby" name="orderby" id="orderby" class="form-control">
                            <option value="">默认排序</option>
                            <optgroup label="降序">
                                <?php echo self::$orderBy['DESC']; ?>
                            </optgroup>
                            <optgroup label="升序">
                                <?php echo self::$orderBy['ASC']; ?>
                            </optgroup>
                        </select>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-lg-3 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="pageSize">每页</label>
                        </div>
                        <input class="form-control" type="text" name="pageSize" id="pageSize" value="<?php echo Paging::$pageSize; ?>" />
                        <div class="input-group-append">
                            <label class="input-group-text" for="pageSize">条记录</label>
                        </div>
                    </div>
                    <div class="input-group input-group-sm col-sm-6 col-lg-3 mt-1">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="keywords">关键字</label>
                        </div>
                        <input type="text" name="keywords" class="form-control" id="keywords" value="<?php echo $_GET['keywords']; ?>" />
                        <div class="input-group-append">
                            <button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-search"></i> 搜 索</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="block">
        <div class="block-header">
            <h3 class="block-title">标签列表</h3>
            <div class="block-options">
                <div class="block-options-item">
                    <span title="总共<?php echo Paging::$count; ?>个标签" class="badge badge-primary"><?php echo Paging::$count; ?></span>
                </div>
            </div>
        </div>
        <div class="block-content p-0">
            <form action="<?php echo APP_URL; ?>&action=batch" method="POST" id="<?php echo APP_FORMID; ?>" target="iPHP_FRAME">
                <div class="table-responsive">
                    <table class="dataTable table text-nowrap table-hover table-striped table-vcenter font-size-sm mb-0">
                        <thead class="bg-light text-black">
                            <tr>
                                <th style="width: 36px;" class="align-middle">
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" class="custom-control-input" id="check-all" data-toggle="checkAll" data-target="#<?php echo APP_FORMID; ?>">
                                        <label class="custom-control-label" for="check-all"></label>
                                    </div>
                                </th>
                                <th>ID</th>
                                <th style="width: 60px;">排序</th>
                                <th>标签名称(使用数)</th>
                                <th>来源字段</th>
                                <th>栏目</th>
                                <th>分类</th>
                                <th>属性</th>
                                <th style="width:48px;">统计</th>
                                <th>
                                    <a class="fa fa-fw fa-clock-alt tip-top" title="更新时间/创建时间"></a>
                                </th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $nodeArray  = Node::instance()->many($result, 'cid');
                            $tnodeArray = Node::many($result, 'tcid', self::$appId);

                            if ($result) foreach ($result as $key => $value) {
                                $C             = (array) $nodeArray[$value['cid']];
                                $TC            = (array) $tnodeArray[$value['tcid']];
                                $iurl          = Route::get('tag', array($value, $C, $TC));
                                $value['url'] = $iurl->href;
                            ?>
                                <tr id="id<?php echo $value['id']; ?>">
                                    <td>
                                        <div class="custom-control custom-checkbox">
                                            <input type="checkbox" class="custom-control-input" id="checkbox-<?php echo $value['id']; ?>" name="id[]" value="<?php echo $value['id']; ?>" />
                                            <label class="custom-control-label" for="checkbox-<?php echo $value['id']; ?>"></label>
                                        </div>
                                    </td>
                                    <td>
                                        <?php echo $value['id']; ?>
                                    </td>
                                    <td class="sortnum"><input type="text" class="form-control form-control-sm" name="sortnum[<?php echo $value['id']; ?>]" value="<?php echo $value['sortnum']; ?>" tid="<?php echo $value['id']; ?>" /></td>
                                    <td>
                                        <?php $value['haspic'] && print '<i class="si fa-fw si-picture"></i>';?>
                                        <a href="<?php echo $value['url']; ?>" class="aTitle" target="_blank">
                                            <?php echo $value['name']; ?>
                                        </a>
                                        <span class="badge badge-info"><?php echo $value['count']; ?></span>
                                        <?php if ($value['haspic']) { ?>
                                            <a href="<?php echo APP_URL; ?>&do=preview&id=<?php echo $value['id']; ?>" data-toggle="modal" title="预览"><img src="<?php echo FilesClient::getPath($value['pic']); ?>" style="height:120px;" /></a>
                                        <?php } ?>
                                    </td>
                                    <td>
                                        <a href="<?php echo APP_DOURL; ?>&field=<?php echo $value['field']; ?><?php echo $uri; ?>">
                                            <?php echo $value['field']; ?>
                                        </a>
                                    </td>
                                    <td>
                                        <a href="<?php echo APP_DOURL; ?>&cid=<?php echo $value['cid']; ?><?php echo $uri; ?>">
                                            <?php echo $C['name']; ?>
                                        </a>
                                        <a href="<?php echo ADMINCP_URL; ?>=article_category&do=add&cid=<?php echo $value['cid']; ?>" target="_blank"><i class="fa fa-fw fa-pencil-alt"></i></a>
                                    </td>
                                    <td>
                                        <?php if ($value['tcid']) { ?>
                                            <a href="<?php echo APP_DOURL; ?>&tcid=<?php echo $value['tcid']; ?><?php echo $uri; ?>">
                                                <?php echo $TC['name']; ?>
                                            </a>
                                            <a href="<?php echo ADMINCP_URL; ?>=tag_node&do=add&cid=<?php echo $value['tcid']; ?>" target="_blank"><i class="fa fa-fw fa-pencil-alt"></i></a>
                                        <?php } ?>
                                    </td>
                                    <td>
                                        <?php $value['pid'] && PropWidget::flag($value['pid'], 'pid'); ?>
                                    </td>
                                    <td>
                                        <a href="javascript:;" class="btn btn-sm btn-light"
                                        data-toggle="popover"
                                        data-html="true"
                                        data-placement="top"
                                        title="统计数据"
                                        data-content='
                                            <div class="row col-12 text-left">
                                            <span class="col-6">总点击数</span> <span class="col-6"><?php echo $value['hits'];?></a></span>
                                            <span class="col-6">当天点击数</span> <span class="col-6"><?php echo $value['hits_today'];?></a></span>
                                            <span class="col-6">昨天点击数</span> <span class="col-6"><?php echo $value['hits_yday'];?></a></span>
                                            <span class="col-6">周点击</span> <span class="col-6"><?php echo $value['hits_week'];?></a></span>
                                            <span class="col-6">月点击</span> <span class="col-6"><?php echo $value['hits_month'];?></a></span>
                                            <hr />
                                            <span class="col-6">使用数</span> <span class="col-6"><?php echo $value['count'];?></a></span>
                                            <span class="col-6">赞</span> <span class="col-6"><?php echo $value['good'];?></a></span>
                                            <span class="col-6">踩</span> <span class="col-6"><?php echo $value['bad'];?></a></span>
                                            <span class="col-6">评论</span> <span class="col-6"><?php echo $value['comment'];?></a></span>
                                            <span class="col-6">收藏</span> <span class="col-6"><?php echo $value['favorite'];?></a></span>
                                            </div>
                                        '
                                        >
                                        <?php echo $value['hits']; ?>
                                        </a>
                                    </td>
                                    <td>
                                        <?php echo get_date($value['pubdate'], 'Y-m-d H:i'); ?><br />
                                        <?php echo get_date($value['postime'], 'Y-m-d H:i'); ?>
                                    </td>
                                    <td class="text-right">
                                        <div class="btn-group btn-group-sm">
                                            <?php if ($value['status'] == "1") { ?>
                                                <a href="<?php echo APP_URL; ?>&action=update&id=<?php echo $value['id']; ?>&_args=status;0" class="btn btn-alt-primary" target="iPHP_FRAME" title="当前状态:启用,点击可禁用此标签"><i class="fa fa-fw fa-power-off"></i></a>
                                                <a href="<?php echo ADMINCP_URL; ?>=keywords&do=add&keyword=<?php echo $value['name']; ?>&url=<?php echo $value['url']; ?>" class="btn btn-alt-primary "><i class="fa fa-fw fa-paperclip"></i></a>
                                                <!-- <a href="<?php echo APP_URL; ?>&action=cache&id=<?php echo $value['id']; ?>" class="btn btn-sm" target="iPHP_FRAME"><i class="fa fa-fw fa-refresh"></i> 更新缓存</a> -->
                                            <?php } ?>
                                            <?php if ($value['status'] == "0") { ?>
                                                <a href="<?php echo APP_URL; ?>&action=update&id=<?php echo $value['id']; ?>&_args=status;1" class="btn btn-alt-primary " target="iPHP_FRAME" title="当前状态:禁用,点击可启用此标签"><i class="fa fa-fw fa-play-circle"></i></a>
                                            <?php } ?>
                                            <a href="<?php echo APP_URL; ?>&do=add&id=<?php echo $value['id']; ?>" class="btn btn-alt-primary "><i class="fa fa-fw fa-pencil-alt"></i></a>
                                            <a href="<?php echo APP_URL; ?>&action=delete&id=<?php echo $value['id']; ?>" target="iPHP_FRAME" class="btn btn-alt-danger" title='永久删除' data-action="delete" /><i class="fa fa-fw fa-trash-alt"></i></a>
                                        </div>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <?php self::formFoot(); ?>
            </form>
        </div>
    </div>
</div>
<div class="iCMS-batch">
    <div id="tplBatch">
        <div class="input-group">
            <div class="input-group-prepend input-group-append">
                <span class="input-group-text">标签模板</span>
            </div>
            <input type="text" name="mtpl" class="form-control" id="mtpl" value="" />
            <?php echo FilesWidget::modalBtn('模板', 'mtpl'); ?>
        </div>
    </div>
    <div id="rootidBatch">
        <div class="input-group">
            <div class="input-group-prepend">
                <span class="input-group-text">父标签ID</span>
            </div>
            <input type="text" class="form-control" name="mrootid" />
        </div>
    </div>
</div>
<div id="import_wrap" style="display:none;width:450px;">
    <form action="<?php echo APP_URL; ?>&action=import" method="POST" target="iPHP_FRAME">
        <div class="form-group row">
            <label class="col-sm-1 col-form-label">栏目</label>
            <div class="col-sm-8">
                <select data-toggle="chosen" name="cid" class="form-control" multiple="multiple" data-placeholder="请选择栏目(可多选)...">
                    <option value="0">请选择标签所属栏目</option>
                    <?php echo $cid_select; ?>
                </select>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-sm-1 col-form-label">分类</label>
            <div class="col-sm-8">
                <select data-toggle="chosen" name="tcid" class="form-control" multiple="multiple" data-placeholder="请选择分类(可多选)...">
                    <option value="0">默认分类</option>
                    <?php echo $tcid_select; ?>
                </select>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-sm-1 col-form-label">属性</label>
            <div class="col-sm-8">
                <select v-model="pid" name="pid" id="pid"  class="form-control" multiple="multiple" data-placeholder="请选择属性(可多选)...">
                    <option value="0">默认属性</option>
                    <?php echo $pid_option; ?>
                </select>
            </div>
        </div>

        <div class="alert alert-info">
            只允许导入TXT文件
            <br /> 每行一个标签
            <br /> 请把文件编码转换成UTF-8
        </div>

        <button type="button" id="local" class="btn btn-primary btn-lg btn-block"><i class="fa fa-fw fa-upload"></i> 请选择要导入的标签</button>
        <input id="localfile" name="upfile" type="file" class="form-control hide" />

    </form>
</div>
<?php self::foot(); ?>